package net.zhuike.novel.app.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BookFonts;
import net.zhuike.novel.app.service.UserService;
import net.zhuike.novel.app.ui.adapter.DownloadFontsAdapter;
import net.zhuike.novel.app.ui.weight.itemdecoration.DefaultItemDecoration;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/6/20.
 *  字体管理
 */

public class FontActivity extends UeBaseActivity {
    @Bind(R.id.refresh_rv_content)
    RecyclerView mRvContent;
    private DownloadFontsAdapter mDownloadAdapter;
    private List<BookFonts>bookFontsList=new ArrayList<>();
    private Context mContext;
    private Handler handler=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_font);
        hiddenFooter();
        showGoBackBtn();
        setTitleText("字体管理");
        mContext=this;
        handler=new Handler();
        mRvContent.setLayoutManager(new LinearLayoutManager(FontActivity.this));
        mRvContent.addItemDecoration(new DefaultItemDecoration(FontActivity.this));
        mDownloadAdapter=new DownloadFontsAdapter(FontActivity.this);

        mRvContent.setAdapter(mDownloadAdapter);
        getFont();
    }

    public void getFont(){
        UserService.getFont(context,new ResponseCallback<BookFonts>() {
            @Override
            public void onSuccess(BookFonts bookFonts) {
                bookFontsList.clear();
                if(bookFonts!=null) {
                    bookFontsList=bookFonts.getFonts();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            handler.post(runnableUi);

                        }
                    }).start();

                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("字体");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("字体");
        MobclickAgent.onPause(context);
    }
    // 构建Runnable对象，在runnable中更新界面
    Runnable   runnableUi=new  Runnable(){
        @Override
        public void run() {
            //更新界面
            if(bookFontsList!=null) {
                mDownloadAdapter.setData(bookFontsList);
                mDownloadAdapter.notifyDataSetChanged();
            }
        }

    };
}
